@darwinanddavis
R Studio in-built terminal connected to your GithubRR workflow example with git
POORGOODN <- 20 # set rep number p <- rep(rnorm(100),N) # repeat a random normal dist of 100 integers N times
change working dir to 'Documents'cd ~/Documents
move one level upcd ..
print current working dirpwd
list files in working dirls
make new working dirmkdir newfolder
create new filetouch text.txt
copy files from source to destination. e.g. cp /Users/mydir/README.txt ~/Documentscp source destination
copy all folders, subfolders, and files from source to destinationcp -R source destination
move files or folders from source to destination (no need for -R)mv source destination
move multiple files with the * wildcard, which copies all .rtf files. The tilde (~) symbol is a shortcut for your Home folder, which contains '/Desktop'.cp ~/Desktop/*.rtf ~/Documents
rename filesmv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtfcp ~/Desktop/MyFile.rtf ~/Documents/MyFile-old.rtf
add and commit your filespush your files to your GithubInitialise your new local repo
# initialise your local git git init
Add the files in your folder to the local git repo
git add . # the '.' adds everything
Stage the files for the commit
git commit -m 'init commit' # -m adds a message
Let's check the changes
git log # recent git activity git add test.txt # adds individual files git status # check what git is doing
Now we push the changes we made from our local repo to our Github cloud.
First, copy the Github repo link you want to push to. Select either https or SSH (requires key access).
Then push your staged (commit) files from your local repo to the remote repo
# set the new remote repo git remote set-url origin "your github repo" # see what remote repositories you have git remote -v # push changes to remote origin (github) from master branch (local) git push origin master
This creates a git repository on your local machine complete with version control.
Every version of every file for the history of the project is grabbed by default when you run git clone.
git clone "github url" "new repo name (optional)" # e.g. git clone https://github.com/darwinanddavis/UsefulCode mynewrepo
git init # initialise your local git git add . # adds all files to git. replace '.' with filename for individ files git commit -m 'redo intro' # '-m' = message
# after the above steps ^ # see what remote repo you have. if a github one exists, you can just push git remote -v # set the new remote repo (if necessary) git remote add origin "your github repo" # if remote branch doesn't exist git remote set-url origin "your github repo" # if already exists # push changes from local repo to remote repo git push -u origin master
fatal: remote origin already exists
The remote origin already exists, so you can't add it again
git remote rm origin # if origin already exists, remove it git remote add origin "your github repo" # then re-add git push origin master # then push again
! [rejected] master -> master (non-fast-forward) Someone else has made changes since your latest ones and git refuses to lose the commit, so won't push your new changes
git pull origin master # fetches any updates to online repo and merges them
fatal: refusing to merge unrelated histories Usually associated with a README file on the Github repo
git pull origin master --allow-unrelated-histories # unnecessary parallel history # merged to your project. usually associated with a README.md file
If VIM opens, type 'SHIFT + :', then press ENTER
Re-do a commit
git reset --soft HEAD~1
Alternative push option
# option 1 git remote set-url origin "link to existing github repo" # talk to github git push -u origin master # option 2 git remote add github "your github repo" # if remote branch doesn't exist git push -u github master
After pushing to your remote repo and this error appears:! [rejected] master -> master (fetch first)
git fetch origin master # match the local repo commit status to the push destination git merge master # merge the recent commits git push -u origin master # push to remote repo # ------- for non-fast-forward error --------- # grab changes made on remote repo and align with local master branch git fetch origin master:tmp git rebase tmp git push origin HEAD:master # push the changes from local HEAD to remote git branch -D tmp git push -u origin master # finalise the changes
For fatal: refusing to merge unrelated histories error
git checkout master git merge origin/master --allow-unrelated-histories # or run this before your 'git pull origin master' command git pull --allow-unrelated-histories origin master
Delete files from a Github repo
# ensure you are in the default branch: git checkout master # the rm -r command will recursively remove your folder: git rm -r folder-name #Commit the change: git commit -m "Remove duplicated directory" # push the change to your remote repo git push origin master
Already on 'master'
M .Rproj.user/ABE7B653/console06/587CA191
M .Rproj.user/ABE7B653/sources/prop/INDEX
M .Rproj.user/shared/notebooks/paths
M billnye.gif
M github_presentation.Rmd
M github_presentation.html
M installing_git.Rmd
M installing_git.html
M installing_git.pdf
M installing_git.tex
Your branch is up-to-date with 'origin/master'.
fatal: pathspec 'folder-name' did not match any files
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
modified: .Rproj.user/ABE7B653/console06/587CA191
modified: .Rproj.user/ABE7B653/sources/prop/INDEX
modified: .Rproj.user/shared/notebooks/paths
modified: billnye.gif
modified: github_presentation.Rmd
modified: github_presentation.html
modified: installing_git.Rmd
modified: installing_git.html
modified: installing_git.pdf
modified: installing_git.tex
Untracked files:
R_is_dope.Rmd
R_is_dope.html
R_is_dope.pdf
R_is_dope.tex
billnye.png
github_presentation_part2.Rmd
github_presentation_part2.html
no changes added to commit
Everything up-to-date
If Github questions your user credentials.
git config --global user.email "<your email>" git config --global user.name "<your github user name>"
When using SSH for your github remote repo, e.g. git@github.com:username/reponame.git
Accessing your SSH key:
- In Mac, in Terminal, type
cat ~/.ssh/id_rsa.pub
ls ~/.ssh/*.pub
How to access recent commits to your local repo
git log # check recent activity and select commit e.g. 0df4g3 ... git checkout "your commit" git checkout master # return to current branch
How to access recent commits to your local repo
Origin master - rejected (fetch first), no file in GitHub repository
Intro to git: Branches, pull requests, and other useful stuff